<?php
/**
 * huhaiqin,2024.10.12
 * 游戏模块后台页面控制器
 */
namespace Game\Controller;

class AdminController extends \Common\Controller\AdminBaseController{
    
    /**
     * huhaiqin
     * 模块首页
     */
    public function index(){
        $this->display();
    }

    /**
     * huhaiqin
     * 预约管理
     */
    public function yuyue(){
        $this->display();
    }

    /**
     * huhaiqin
     * 预约编辑
     */
    public function yuyue_edit(){
        C('LAYOUT_NAME','layout_layui');
        if($_GET['id']){
            $m = D('GameYuyue');
            $this->info = $m->find($_GET['id']);
            $this->title = '修改预约';
        }else{
            $this->title = '添加预约';
        }
        $this->display();
    }

    /**
     * huhaiqin
     * 预约用户
     */
    public function yuyue_user(){
        C('LAYOUT_NAME','layout_layui');
        $this->display();
    }

    /**
     * huhaiqin
     * 预约用户编辑
     */
    public function yuyue_user_edit(){
        C('LAYOUT_NAME','layout_layui');
        if($_GET['id']){
            $m = D('GameYuyueUser');
            $this->info = $m->find($_GET['id']);
            $this->title = '修改预约';
        }else{
            $this->title = '添加预约';
        }
        $this->display();
    }

    /**
     * huhaiqin
     * 福利码管理
     */
    public function code_reward(){
        $this->display();
    }

    public function code_reward_edit(){
        C('LAYOUT_NAME','layout_layui');
        if($_GET['id']){
            $m = D('GameCodeReward');
            $this->info = $m->find($_GET['id']);
            $this->title = '修改福利码';
        }else{
            $this->title = '添加福利码';
        }
        $this->display();
    }

    /**
     * huhaiqin，2023.04.03
     * 通知图片管理
     */
    public function notice_image(){
        $this->display();
    }

    /**
     * huhaiqin，2023.04.03
     * 通知图片编辑
     */
    public function notice_image_edit(){
        C('LAYOUT_NAME','layout_layui');
        if($_GET['id']){
            $m = D('GameNoticeImage');
            $this->info = $m->find($_GET['id']);
            $this->title = '修改通知图片';
        }else{
            $this->title = '添加通知图片';
        }
        $this->display();
    }

    /**
     * huhaiqin，2023.04.03
     * 页面模块管理
     */
    public function module(){
        $this->display();
    }

    /**
     * huhaiqin，2023.04.03
     * 添加，编辑页面模块
     */
    public function module_edit(){
        C('LAYOUT_NAME','layout_layui');
        $info = [];
        if($_GET['id']){
            $info = D('PageModule')->find($_GET['id']);
        }
        $this->info = $info;
        $this->display();
    }

    /**
     * huhaiqin
     * 游戏地图编辑器
     */
    public function editor(){
        C('LAYOUT_NAME','layout_layui');
        $this->title = '游戏地图编辑器';
        $this->display();   
    }

     /**
     * huhaiqin，2023.04.08
     * 图标选择页面
     */
    public function iconfont(){
        C('LAYOUT_NAME','layout_layui');
        $this->display();
    }

    public function config_read(){
        $res = D('GameMapConfig')->get_config_data(I('stage_id')); 
        if(!$res){
            _apiReturn('0', '获取失败'); 
            return;
        }
        $config = $res['config_data'];
        $config = html_entity_decode($config);
        // dump($config);
        // $config = json_decode($config, true);
        $res['config_data'] = json_decode($config, true);
        _apiReturn('1', '获取成功', $res);
    }

    public function config_save(){
        $data = I('post.');
        if(!$data['stage_id']){
            _apiReturn('0', '缺少stageId');
            return;
        }
        $data['config_data'] = htmlspecialchars_decode($data['config_data']);
        // foreach($data['config_data'] as $k=>$v){
        //     if(is_array($v)){
        //         $data['config_data'][$k] = json_encode($v, JSON_UNESCAPED_UNICODE);
        //     } 
        // }
        // dump($data);
        // $data['config_data'] = json_encode($data['config_data'], JSON_UNESCAPED_UNICODE);s
        // dump($data);die();
        $res = D('GameMapConfig')->save_config_data($data);
        if(!$res){
            _apiReturn('0', '保存失败');
            return;
        }
        _apiReturn('1', '保存成功');
    }

    public function config_generate(){
        // 抢鲜版放在stage里，正式版放在Stages里
        $map_path = I('is_trial') == 'trial' ? 'stage/' : 'Stages/';
        $required_key = [
            'stageId', 'length', 'mapType', 'speed', 'baseCol', 'speedRate'
        ];
        // 预处理数据结构
        $data = I('data');
        $data = htmlspecialchars_decode($data);
        $data= json_decode($data, true);
        foreach($data as $k=>$item){
            foreach($item as $k2=>$v2){
                if(json_decode($v2) !== null){
                    $data[$k][$k2] = json_decode($v2, true);
                }
            }
        }
        $config = [];
        $track_config = [];
        // 此时的$data是关联数组
        foreach($data as $k=>$v){
            // unset($data[$k]['component']);
            // 检查时配置项还是效果项
            if($v['type'] == 'config'){
                unset($v['type']);
                unset($v['component']);
                foreach($v as $k2=>$v2){
                    if($v2){
                        $config['config'][$k2] = $v2;
                    }
                }
                unset($data[$k]);
            }
            // 顺便把几跑道也做了
            if($v['type'] == 'trackconfig'){
                $tmp = [
                    'pos' => $v['pos'],
                    'length' => $v['length'],
                    'col' => $v['col']
                ];
                $track_config[] = $tmp;
                unset($data[$k]['type']);
            }
            // 把已经是标准格式的道具配置项拿出来
            if($v['type'] == 'json'){
                // 因为内部的pos是相对坐标，还要加上前面的pos
                foreach($v['data'] as $k3=>$v3){
                    $v3['pos'] += $v['pos'];
                    $data[] = $v3; 
                }
                unset($data[$k]);
            }
        }
        $map_items = [];
        foreach($data as $k=>$v){
            $item = [];
            // POS转成数组并转成int
            $item['pos'] = explode(',',$v['pos']);
            $item['pos'] = array_map('floatval', $item['pos']);
            // 找出对应的col
            // 如果没设置分段COL，就用baseCol
            $item['col'] = $config['config']['baseCol'];
            if($track_config){
                foreach($track_config as $k2=>$v2){
                    if(($item['pos'][0] >= $v2['pos']) && ($item['pos'][0] <= ($v2['pos'] + $v2['length']))){
                        $item['col'] = $v2['col'];
                        break;
                    } 
                }
            }
            unset($v['pos']);
            // unset($v['col']);
            // X转成数组并转成int
            if($v['x']){
                $item['x'] = explode(',', $v['x']);
                $item['x'] = array_map('intval', $item['x']);
            }else{
                // 没配置就随机
                $item['x'] = range(1, $item['col']);
            }
            unset($v['x']);
            $item['type'] = $v['component'];
            unset($v['component']);
            // 针对障碍物来，把obstacleConfig转成数组
            if($item['type'] == 'obstacle'){
                $v['obstacleConfig'] = explode(',', $v['obstacleConfig']);
                $v['obstacleConfig'] = array_map('intval', $v['obstacleConfig']);
            }
            // 针对算术门，把每个gate里的配置项转成数组
            if($item['type'] == 'gateCalc2'){
                $v['gateConfig'] = [];
                for($i = 1; $i <= 4; $i ++){
                    if($v['gate' . strval($i)]){
                        $new_config = explode(',', $v['gate' . strval($i)]);
                        $v['gateConfig'][] = $new_config;
                    }
                }
            }
            $item['params'] = $v;
            $map_items[] = $item;
        }
        usort($map_items, function($a, $b){
            return $a['pos'][0] - $b['pos'][0]; 
        });
        $config['items'] = $map_items;
        // 检查必填项
        // foreach($required_key as $k=>$v){
        //     if(!isset($config['config'][$v])){
        //         $this->error('缺少必填项：'.$v);
        //     }
        // }
        // 检查是否存在
        $json = json_encode($config,JSON_UNESCAPED_UNICODE);
        $json = stripslashes($json);
        $filepath = 'GameConfig/' . $map_path . $config['config']['stageId']. '.json';
        $res = file_put_contents($filepath, $json. "\n");
        if($res){
            _apiReturn('1', '生成成功，路径:' . $filepath);
        }else{
            _apiReturn('0', '生成失败，路径:' . $filepath);
        }
    }
}